package com.hwd.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.hwd.gmall.payment.service.ZfbPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 支付宝支付模块的的接口实现类
 *
 * @author 黄伟东/Victor
 * @date 2022/5/11 1:00
 */
@Service
public class ZfbPayServiceImpl implements ZfbPayService {

    @Value("${alipay.alipay_url}")
    private String alipayUrl;

    @Value("${alipay.app_id}")
    private String appId;

    @Value("${alipay.app_private_key}")
    private String appPrivateKey;

    @Value("${alipay.alipay_public_key}")
    private String alipayPublicKey;

    @Value("${alipay.return_payment_url}")
    private String returnPaymentUrl;

    @Value("${alipay.notify_payment_url}")
    private String notifyPaymentUrl;


    /**
     * 获取支付宝支付二维码的接口
     *
     * @param orderId     订单id
     * @param money       订单金额
     * @param description 订单描述
     * @return java.lang.String
     */
    @Override
    public String getZfbPayUrl(String orderId, String money, String description) {
        // 支付宝支付客户端对象初始化
        AlipayClient alipayClient =
                new DefaultAlipayClient(alipayUrl, appId, appPrivateKey,
                        "json", "UTF-8", alipayPublicKey, "RSA2");
        // 声明请求头
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 设置通知地址
        request.setNotifyUrl(notifyPaymentUrl);
        // 设置回调地址
        request.setReturnUrl(returnPaymentUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        bizContent.put("total_amount", money);
        bizContent.put("subject", description);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        // 设置请求参数
        request.setBizContent(bizContent.toString());

        try {
            // 发起请求，获取结果
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            if (response.isSuccess()) {
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取订单的支付结果
     *
     * @param orderId 订单id
     * @return java.lang.String
     */
    @Override
    public String getZfbPayResult(String orderId) {
        // 支付宝支付客户端对象初始化
        AlipayClient alipayClient =
                new DefaultAlipayClient(alipayUrl, appId, appPrivateKey,
                        "json", "UTF-8", alipayPublicKey, "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);

        request.setBizContent(bizContent.toString());
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭交易
     *
     * @param orderId 订单id
     * @return java.util.Map<java.lang.String, java.lang.String>
     */
    @Override
    public Map<String, String> closePay(String orderId) {
        return null;
    }
}
